package com.das.mechanic_base.adapter.groundpush;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.ground.UserCarInfoBean;
import com.das.mechanic_base.utils.X3GlideRoundTransform;
import com.das.mechanic_base.utils.X3StringUtils;

import java.util.ArrayList;
import java.util.List;

public class X3ServiceCarInfoAdapter extends RecyclerView.Adapter<X3ServiceCarInfoAdapter.ServiceCarInfoHolder> {

    private List<UserCarInfoBean> infoBeans;

    private Context mContext;

    private List<Boolean> groupList;

    private final RequestOptions myOptions;
    public X3ServiceCarInfoAdapter(Context mContext) {
        this.infoBeans = new ArrayList<>();
        this.mContext = mContext;
        groupList = new ArrayList<>();

        myOptions = new RequestOptions()
                .centerCrop()
                .dontAnimate()
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .transform(new X3GlideRoundTransform(mContext, 4));
    }

    public void changeGroup (List<UserCarInfoBean> groupOrTimeBeans) {
        this.infoBeans = groupOrTimeBeans;
        groupList.clear();
        for (int i = 0; i < groupOrTimeBeans.size(); i++) {
            groupList.add(false);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ServiceCarInfoHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ServiceCarInfoHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_service_car_info_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull ServiceCarInfoHolder holder, int position) {
        if (groupList.get(position)) {
            // 点击选中
            holder.itemView.setBackground(ContextCompat.getDrawable(mContext,R.drawable.x3_group_or_time_bg));
        }else {
            // 点击取消选中
            holder.itemView.setBackground(ContextCompat.getDrawable(mContext,R.drawable.x3_shape_idea_unselect_item));
        }
        // 加载车辆头像
        Glide.with(mContext).load(X3StringUtils.getImageUrl(infoBeans.get(position).getCarBrandImgUrl())).apply(new RequestOptions().
                apply(myOptions).error(R.mipmap.x3_car_no_img).
                placeholder(R.mipmap.x3_car_no_img)).into(holder.iv_brand);

        holder.tv_num.setText(infoBeans.get(position).getNum() + "");
        holder.tv_style.setText(infoBeans.get(position).getCarBrandName()+"-"+infoBeans.get(position).getCarStyleName());

        holder.itemView.setOnClickListener(view -> {
            for (int i = 0; i < groupList.size(); i++) {
                if (i == position) {
                    if (!groupList.get(position)) {
                        groupList.remove(position);
                        groupList.add(position,true);
                    }
                }else {
                    if (groupList.get(i)) {
                        groupList.remove(i);
                        groupList.add(i,false);
                    }
                }
            }
            if (iOnClickCar != null) {
                iOnClickCar.iOnClickCarNext(infoBeans.get(position).getBrandId(),infoBeans.get(position).getStyleId(),
                        infoBeans.get(position).getId(),infoBeans.get(position).getCarBrandName()+"-"+infoBeans.get(position).getCarStyleName());
            }
            notifyDataSetChanged();
        });
    }
    IOnClickCar iOnClickCar;

    public void setiOnClickCar(IOnClickCar iOnClickCar) {
        this.iOnClickCar = iOnClickCar;
    }

    public interface IOnClickCar{

        void iOnClickCarNext(long brandId,long carStyleId,long carId,String carStyle);
    }

    @Override
    public int getItemCount() {
        return infoBeans.size();
    }

    class ServiceCarInfoHolder extends RecyclerView.ViewHolder {
        ImageView iv_brand;
        TextView tv_num;
        TextView tv_style;
        public ServiceCarInfoHolder(@NonNull View itemView) {
            super(itemView);
            iv_brand = itemView.findViewById(R.id.iv_brand);
            tv_num = itemView.findViewById(R.id.tv_num);
            tv_style = itemView.findViewById(R.id.tv_style);
        }
    }
}
